package com.lucian.ge.controller.shopportal;

import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.comman.model.ResultMapSyn;
import com.lucian.ge.service.shop.ShippingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lucicol on 2019/7/12.
 */
@Api(value = "ShippingController", description = "Shipping-物流接口")

@RestController
@RequestMapping("/shipping")
public class ShippingController {
    private final ResultMap resultMap;

    @Value("${com.appcode}")
    private String appcode;

    @Autowired
    public ShippingController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

//    @Autowired(required=false)
//    private ShippingService shippingService;

    /**
     * 获取快递信息
     */
    @ApiOperation(value="获取快递信息", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @GetMapping("/getwuliu")
    @ResponseBody
    public ResultMap confirm_receipt (@RequestHeader String token,
                                         @RequestParam(value = "userid") Integer userId,
                                         @RequestParam(value = "wuliuno") String wuliuNo) {
        if (!GetAuthController.getUserAuth(token, userId).equals("普通注册用户验证通过")) {
            return resultMap.success().code(301).message(GetAuthController.getUserAuth(token, userId)).token("").res("").data(null);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "APPCODE " + appcode);

        HttpEntity entity = new HttpEntity(headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> result = restTemplate.exchange("https://wuliu.market.alicloudapi.com/kdi?no=" + wuliuNo, HttpMethod.GET, entity , String.class);
//        System.out.println(result);

        return resultMap.success().code(200).message("获取快递信息成功").token("").res(result.getBody()).data(null);
    }
}
